package dao

import (
	"github.com/go-redis/redis"
	"go-learn/global"
)

func ZAdd(key string, members []redis.Z) error {
	err := global.GvaRedis.ZAdd(key, members...).Err()
	return err
}
func ZRem(key string, members []interface{}) error {
	err := global.GvaRedis.ZRem(key, members...).Err()
	return err
}

/*
	获取排行榜并分页
*/
func ZRevRangeWithScores(key string, page, size int64) ([]redis.Z, error) {
	start := (page - 1) * size
	end := start + size - 1
	result, err := global.GvaRedis.ZRevRangeWithScores(key, start, end).Result()
	return result, err
}

/*
	获取指定分数区间的排行榜并分页
*/
func ZRevRangeByScoreWithScores(key, min, max string, page, size int64) ([]string, error) {
	var offset int64
	if page != 0 && size != 0 {
		offset = (page - 1) * size
	}
	rangeby := redis.ZRangeBy{Min: min, Max: max, Offset: offset, Count: size}
	result, err := global.GvaRedis.ZRevRangeByScore(key, rangeby).Result()
	return result, err
}

/*
	查询指定人的排名
*/
func ZRevRank(key, member string) (int64, error) {
	result, err := global.GvaRedis.ZRevRank(key, member).Result()
	return result, err
}
